﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using yeubongda.Models;

namespace yeubongda.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            ViewBag.Message = "Welcome to ASP.NET MVC!";
            return View();
        }

        public ActionResult BaiBaoTheoMenuCha(int id)
        {
            ybdEntities1 ybd = new ybdEntities1();
            var query1 = from menu in ybd.MENUs
                         where menu.MAMN == id
                         select menu;
            ViewBag.MaMNCha = query1.First();

            ybdEntities1 context = new ybdEntities1();
            var query = from baibao in context.BAIBAOs
                        where baibao.MaMN == id
                        select baibao;
            return View();
        }
        public ActionResult BaiBaoDetail(int id)
        {
            ybdEntities1 context = new ybdEntities1();
            var query = from baibao in context.BAIBAOs
                        where baibao.MABAI == id
                        select baibao;
            ViewBag.baibao = query.First();

            return View();
        }
        public ActionResult BaiBaoTheoMenuCon(int id)
        {
            ViewBag.MaMNCon = YBDModels.GetMenu(id);
            // phan trang
            int page = 1;
            if(Request.QueryString["page"]!=null)
            {
                page = Convert.ToInt32(Request.QueryString["page"]);
            }
            int soSanPham1Trang = 10;
            ViewBag.SoTrang = YBDModels.LaySoTrang(soSanPham1Trang, id);
            page = page - 1;
            ViewBag.listBaiBao = YBDModels.LayDanhSach(id, page, soSanPham1Trang);
            ViewBag.TrangHienTai = page + 1;
            return View();
        }
        public JsonResult BaiBaoTheoMenuConPhanTrang(int id)
        {
            ViewBag.MaMNCon = YBDModels.GetMenu(id);
            // phan trang
            int page = 1;
            if (Request.QueryString["page"] != null)
            {
                page = Convert.ToInt32(Request.QueryString["page"]);
            }
            int soSanPham1Trang = 10;
            ViewBag.SoTrang = YBDModels.LaySoTrang(soSanPham1Trang, id);
            page = page - 1;
            ViewBag.listBaiBao = YBDModels.LayDanhSach(id, page, soSanPham1Trang);
            ViewBag.TrangHienTai = page + 1;
            return Json(ViewBag.listBaiBao);
        }
        public ActionResult XuLyTimNhanh(FormCollection form)
        {
            // lấy nội dung từ textbox
            string tenBB = form["txtSeach"];
            ViewBag.TenBB = tenBB;
            int page = 1;
            if (Request.QueryString["page"] != null)
            {
                page = Convert.ToInt32(Request.QueryString["page"]);
            }

            int soSanPham1Trang = 10;
            ViewBag.SoTrang = YBDModels.LaySoTrangTenBB(soSanPham1Trang, tenBB);
            page = page - 1;
            ViewBag.listKetQua = YBDModels.getListByTenBaiBao(tenBB, page, soSanPham1Trang); ;
            ViewBag.TrangHienTai = page + 1;

            return View();
        }
        public JsonResult XuLyTimNhanhPhanTrang()
        {
            // lấy nội dung từ textbox
            string tenBB = "";
            if (Request.QueryString["tenBB"] != null)
            {
                tenBB = Convert.ToString(Request.QueryString["tenBB"]);
            }
            ViewBag.TenBB = tenBB;
            // phan trang
            int page = 1;
            if (Request.QueryString["page"] != null)
            {
                page = Convert.ToInt32(Request.QueryString["page"]);
            }
            int soSanPham1Trang = 10;
            ViewBag.SoTrang = YBDModels.LaySoTrangTenBB(soSanPham1Trang, tenBB);
            page = page - 1;
            ViewBag.listKetQua = YBDModels.getListByTenBaiBao(tenBB, page, soSanPham1Trang); ;
            ViewBag.TrangHienTai = page + 1;

            return Json(ViewBag.listKetQua);
        }

        public ActionResult TimNangCao()
        {
            // chuyển ng dùng đến view với form tìm nâng cao
            List<LOAI> list = YBDModels.GetList();
            ViewBag.listLoai = list;
            return View();

        }

        // ham duoc goi khi ng dung click button tim 
        // 
        [HttpPost]
        public ActionResult TimNangCao(FormCollection form)
        {

            IEnumerable<BAIBAO> listResult = YBDModels.GetListBB();
            int soLuong = listResult.Count();

            // kiem tra cac tieu chi ng dung nhap va thuc hien tim kiem
            string tenBB = form["txtTenBB"];
            if (tenBB != "")
            {
                listResult = (IEnumerable<BAIBAO>)listResult.Where(bb => bb.TENBAI.ToLower().Contains(tenBB.ToLower()) == true);
            }

            if (form["txtTuNgay"] != "")
            {
                int Ngay = Convert.ToInt32(form["txtNgay"]);
                listResult = (IEnumerable<BAIBAO>)listResult.Where(bb => Convert.ToInt32(bb.Ngay) >= Ngay);
            }

            if (form["txtDenNgay"] != "")
            {
                int DenNgay = Convert.ToInt32(form["txtDenNgay"]);
                listResult = (IEnumerable<BAIBAO>)listResult.Where(bb => Convert.ToInt32(bb.Ngay) <= DenNgay);
            }

            int loai = Convert.ToInt32(form["cmbLoai"]);
            if (loai != 0)
            {
                listResult = (IEnumerable<BAIBAO>)listResult.Where(bb => bb.Maloai == loai);

            }

            soLuong = listResult.Count();


            ViewBag.listKQ = listResult;

            List<LOAI> list = YBDModels.GetList();
            ViewBag.listLoai = list;

            return View();
        }
        public ActionResult About()
        {
            
            return View();
        }
    }
}
